angular.module('UCX.Cmis')
.directive('inputEmail',(CmisService, localStorageService, ModelService,$window,$timeout, $uibModal, Utils, $translate)->

  scope:{
    email: '=',
    newEmail: '='
    newUserList: '='
    lang: '='
    placeholder: '='
  }

#  http://codepen.io/prophet_w/pen/GWjejg
  restrict:'E'
#  replace:true
  templateUrl: 'template-path/input-emails.html',
  link:(scope, element, attr)->
    console.log('====scope.email====');
    console.log(scope.email);
    generateUser=(email)->
      # email not only email maybe group name
      for user in scope.newUserList
        if user.userEmail==email
          return
      return {
        userEmail:email
        isValidInput:Utils.isValidEmail(email) || Utils.isInGroup(email)
        avatar:email

      }
    if(scope.lang && scope.lang.indexOf('-')!=-1)
      $translate.use(scope.lang.split('-')[1])
    scope.placeholderTxt=$translate.instant(scope.placeholder)
    scope.newUserList=[]
    scope.addNewUser=(event)->
      if scope.newEmail
        emailList = scope.newEmail.replace(/\s+/g,';');
        emailList = emailList.replace(/\；+/g,';');
        emailList = emailList.replace(/\;+/g,';');
        emailList = emailList.replace(/\,+/g,';');
        emailList = emailList.replace(/\，+/g,';');

        if(emailList.indexOf(';')!=-1)
          emailArr = emailList.split(';')
          for email in emailArr
            if email
              user = generateUser(email)
              if user
                scope.newUserList.push(user)
        else
          user = generateUser(scope.newEmail)
          if user
            scope.newUserList.push(user)
        scope.newEmail=''
        if event
          event.preventDefault()
    scope.deleteUser=(index)->
      scope.newUserList.splice(index,1)

    scope.handleKeyPress=(event)->
  # handle ctrl + v paste
  #    if event.metaKey==true && event.keyCode==86 || event.ctrlKey==true && event.keyCode==86
      if navigator.userAgent.toLowerCase().indexOf('android')>-1
        if event && event.which==13
          if !scope.newEmail
            if event
              event.preventDefault()
          else
            scope.addNewUser()
            $timeout(()->
              $('#user-input-area').val('')
            )
          return false
        return
      # handle 'enter' key 'space' key ';'  ','
      # 中文输入法输入时没有触发 keypress 事件 可以实现中文enter 变成英文 不触发 addNewUser 事件
      # delete 的时候没有触发key press 事件
      if event && event.which==13
        if !scope.newEmail
          if event
            event.preventDefault()
        else
          scope.addNewUser()
          $timeout(()->
            $('#user-input-area').val('')
          )
        return false
    scope.handleKeyUp=(event)->
      console.log('up',event);
      # 安卓 chrome keyup 无法识别 key  直接全部判定为  keyCode==229

      if navigator.userAgent.toLowerCase().indexOf('android')>-1
        if event && event!="Unidentified" && event.keyCode==13
          if !scope.newEmail
            if event
              event.preventDefault()
            return false
          else
            scope.addNewUser()
        return
      # handle 'enter' key 'space' key ';'  ','
      if event && event!="Unidentified" && (event.keyCode==188 || event.keyCode==32 || event.keyCode==186 || event.keyCode==229)
        if !scope.newEmail
          if event
            event.preventDefault()
          return false
        else
          scope.addNewUser()
    scope.$on('add-user',()->
      scope.addNewUser()
    )
    scope.clearNewUserList=()->
      scope.newUserList=[]
      scope.newEmail=''
      scope.feedback=false
      scope.postMessage=''

)
